package com.stylefeng.guns.modular.flagglory.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.common.persistence.model.Division;
import com.stylefeng.guns.common.persistence.dao.DivisionMapper;
import com.stylefeng.guns.modular.flagglory.service.IDivisionService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * lg_段位 服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-01-31
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DivisionServiceImpl extends ServiceImpl<DivisionMapper, Division> implements IDivisionService {
    public Division getDivision(Integer starNumber) {
        EntityWrapper<Division> divisionEntityWrapper = new EntityWrapper<>();
        divisionEntityWrapper.orderBy("star");
        Division division = null;
        List<Division> divisions = selectList(divisionEntityWrapper);
        for (Division div : divisions) {
            if (starNumber >= div.getStar()) {
                division = div;
            } else {
                break;
            }
        }
        return division;
    }

    @Override
    public Division getNextDivision(Integer divisionId) {
        Division division = selectById(divisionId);
        Integer sort = division.getSort();
        EntityWrapper<Division> divisionEntityWrapper = new EntityWrapper<>();
        divisionEntityWrapper.where("sort>" + sort).orderBy("sort");
        Division nextDivision = selectOne(divisionEntityWrapper);
        return nextDivision;
    }
}
